#!/usr/bin/perl -w
#
# $Id: PrefsDialog.pm,v 1.22 2004/03/25 05:24:40 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# PrefsDialog class
#
package PrefsDialog;

use Gnome;
use strict;
use Misc;

#
# Constructor
#
sub new
{
	my $type = shift;
	my $self = {};
	bless $self, $type;

	my $gKismetApplication = shift;
	$self->{'gKismetApplication'} = $gKismetApplication;
	my $preferences = $gKismetApplication->{'preferences'};

	my $propertyBox = new Gnome::PropertyBox();
	$self->{'propertyBox'} = $propertyBox;
	$propertyBox->signal_connect('destroy', sub{$self->destroyHandler(@_)});
	$propertyBox->signal_connect('apply', sub {$self->applyHandler(@_)});
	$propertyBox->set_parent($gKismetApplication->getWidget());

	# Page 0
	my $nNetworkFields = scalar $preferences->getPref('networkFieldNames');
	my $networkFrame = new Gtk::Frame('Network');
	my $networkTable = new Gtk::Table($nNetworkFields + 1, 2, $false);
	$networkFrame->add($networkTable);
	my $i = 0;
	for my $f ($preferences->getPref('networkFieldNames'))
	{
		my $button = new Gtk::CheckButton($f);
		$button->set_active($preferences->getPref('networkFieldStatus', $f));
		$self->{'networkFieldButton'}{$f} = $button;
		$button->signal_connect('clicked', sub {$self->changedHandler()});
		if(!($i % 2))
		{
			$networkTable->attach($button, 0, 1, $i, $i + 1, 'fill', 'fill', 0, 0);
		}
		else
		{
			$networkTable->attach($button, 1, 2, $i - 1, $i, 'fill', 'fill', 0, 0);
		}
		$i++;
	}
	my $nClientFields = scalar $preferences->getPref('clientFieldNames');
	my $clientFrame = new Gtk::Frame('Client');
	my $clientTable = new Gtk::Table($nClientFields + 1, 2, $false);
	$clientFrame->add($clientTable);
	$i = 0;
	for my $f ($preferences->getPref('clientFieldNames'))
	{
		my $button = new Gtk::CheckButton($f);
		$button->set_active($preferences->getPref('clientFieldStatus', $f));
		$self->{'clientFieldButton'}{$f} = $button;
		$button->signal_connect('clicked', sub {$self->changedHandler()});
		if(!($i % 2))
		{
			$clientTable->attach($button, 0, 1, $i, $i + 1, 'fill', 'fill', 0, 0);
		}
		else
		{
			$clientTable->attach($button, 1, 2, $i - 1, $i, 'fill', 'fill', 0, 0);
		}
		$i++;
	}
	my $hbox = new Gtk::HBox($true, 0);
	$hbox->pack_start($networkFrame, $true, $false, 0);
	$hbox->pack_start($clientFrame, $true, $false, 0);
	my $tabLabel0 = new Gtk::Label('Fields');
	$propertyBox->append_page($hbox, $tabLabel0);

	# Page 1
	# Window depths
	my $packetAdjustment = new Gtk::Adjustment($preferences->getPref('packetDepth'), 100, 10000, 100, 1000, 0);
	my $packetSpinButton = new Gtk::SpinButton($packetAdjustment, 0, 0);
	$packetSpinButton->set_numeric($true);
	$packetSpinButton->signal_connect('changed', sub {$self->changedHandler(@_)});
	$self->{'packetSpinButton'} = $packetSpinButton;
	my $stringAdjustment = new Gtk::Adjustment($preferences->getPref('stringDepth'), 100, 10000, 100, 1000, 0);
	my $stringSpinButton = new Gtk::SpinButton($stringAdjustment, 0, 0);
	$stringSpinButton->set_numeric($true);
	$stringSpinButton->signal_connect('changed', sub {$self->changedHandler(@_)});
	$self->{'stringSpinButton'} = $stringSpinButton;
	my $packetLabel = new Gtk::Label('Packet window depth:');
	my $stringLabel = new Gtk::Label('String window depth:');
	my $packetHBox = new Gtk::HBox($true, 0);
	my $stringHBox= new Gtk::HBox($true, 0);
	$packetHBox->pack_start($packetLabel, $true, $true, 20);
	$packetHBox->pack_start($packetSpinButton, $true, $true, 20);
	$stringHBox->pack_start($stringLabel, $true, $true, 20);
	$stringHBox->pack_start($stringSpinButton, $true, $true, 20);
	my $vbox = new Gtk::VBox($false, 0);
	$vbox->pack_start($packetHBox, $false, $false, 20);
	$vbox->pack_start($stringHBox, $false, $false, 20);
	# Units
	my $imperialButton = new Gtk::RadioButton('Imperial units');
	$self->{'imperialButton'} = $imperialButton;
	my $metricButton = new Gtk::RadioButton('Metric units', $imperialButton);
	$self->{'metricButton'} = $metricButton;
	$preferences->getPref('units') eq 'imperial' ? $imperialButton->set_active($true) : $metricButton->set_active($true);
	$imperialButton->signal_connect('clicked', sub {$self->changedHandler()});
	$metricButton->signal_connect('clicked', sub {$self->changedHandler()});
	my $unitVBox = new Gtk::VBox($true, 0);
	$unitVBox->pack_start($imperialButton, $false, $false, 0);
	$unitVBox->pack_start($metricButton, $false, $false, 0);
	my $unitHBox = new Gtk::HBox($false, 0);
	$unitHBox->pack_start($unitVBox, $false, $false, 20);
	$vbox->pack_start($unitHBox, $false, $false, 20);
	# Keep sorted
	my $keepSortedButton = new Gtk::CheckButton('Keep network tree sorted at all times (slow)');
	$keepSortedButton->set_active($preferences->getPref('keepSorted'));
	$self->{'keepSortedButton'} = $keepSortedButton;
	$keepSortedButton->signal_connect('toggled', sub {$self->changedHandler()});
	my $ksHbox = new Gtk::HBox($false, 0);
	$ksHbox->pack_start($keepSortedButton, $false, $false, 20);
	$vbox->pack_start($ksHbox, $false, $false, 20);
	# Assemble
	my $miscFrame = new Gtk::Frame('Misc');
	$miscFrame->add($vbox);
	my $tabLabel1 = new Gtk::Label('Misc');
	$propertyBox->append_page($miscFrame, $tabLabel1);

	# Page 2
	# Play binary
	my $playBinaryHBox = new Gtk::HBox($true, 0);
	my $playBinaryEntry = new Gtk::Entry();
	$self->{'playBinaryEntry'} = $playBinaryEntry;
	$playBinaryEntry->set_text($preferences->getPref('playBinary'));
	$playBinaryEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $playBinaryLabel = new Gtk::Label('Sound play binary');
	$playBinaryHBox->pack_start($playBinaryLabel, $true, $true, 20);
	$playBinaryHBox->pack_end($playBinaryEntry, $true, $true, 20);

	# Network sound
	my $networkSoundHBox = new Gtk::HBox($true, 0);
	my $networkSoundEntry = new Gtk::Entry();
	$self->{'networkSoundEntry'} = $networkSoundEntry;
	$networkSoundEntry->set_text($preferences->getPref('newNetworkSound'));
	$networkSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $networkSoundButton = new Gtk::CheckButton('New network sound');
	$networkSoundButton->set_active($preferences->getPref('networkSoundEnabled'));
	$self->{'networkSoundEnabledButton'} = $networkSoundButton;
	$networkSoundButton->signal_connect('toggled', sub {$networkSoundEntry->set_sensitive($networkSoundButton->active()); $self->changedHandler()});
	$networkSoundEntry->set_sensitive($networkSoundButton->active());
	$networkSoundHBox->pack_start($networkSoundButton, $true, $true, 20);
	$networkSoundHBox->pack_end($networkSoundEntry, $true, $true, 20);

	# Alert sound
	my $alertSoundHBox = new Gtk::HBox($true, 0);
	my $alertSoundEntry = new Gtk::Entry();
	$self->{'alertSoundEntry'} = $alertSoundEntry;
	$alertSoundEntry->set_text($preferences->getPref('alertSound'));
	$alertSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $alertSoundButton = new Gtk::CheckButton('Alert sound');
	$alertSoundButton->set_active($preferences->getPref('alertSoundEnabled'));
	$self->{'alertSoundEnabledButton'} = $alertSoundButton;
	$alertSoundButton->signal_connect('toggled', sub {$alertSoundEntry->set_sensitive($alertSoundButton->active()); $self->changedHandler()});
	$alertSoundEntry->set_sensitive($alertSoundButton->active());
	$alertSoundHBox->pack_start($alertSoundButton, $true, $true, 20);
	$alertSoundHBox->pack_end($alertSoundEntry, $true, $true, 20);

	# GPS sound
	my $gpsAcqSoundEntry = new Gtk::Entry();
	$self->{'gpsAcqSoundEntry'} = $gpsAcqSoundEntry;
	$gpsAcqSoundEntry->set_text($preferences->getPref('gpsAcqSound'));
	$gpsAcqSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $gpsLostSoundEntry = new Gtk::Entry();
	$self->{'gpsLostSoundEntry'} = $gpsLostSoundEntry;
	$gpsLostSoundEntry->set_text($preferences->getPref('gpsLostSound'));
	$gpsLostSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $gpsEntryVBox = new Gtk::VBox($true, 0);
	$gpsEntryVBox->pack_start($gpsAcqSoundEntry, $false, $false, 0);
	$gpsEntryVBox->pack_start($gpsLostSoundEntry, $false, $false, 0);
	my $gpsSoundButton = new Gtk::CheckButton('GPS acquired/lost sound');
	$gpsSoundButton->set_active($preferences->getPref('gpsSoundEnabled'));
	$self->{'gpsSoundEnabledButton'} = $gpsSoundButton;
	$gpsSoundButton->signal_connect('toggled', sub {$gpsAcqSoundEntry->set_sensitive($gpsSoundButton->active());
		$gpsLostSoundEntry->set_sensitive($gpsSoundButton->active()); $self->changedHandler()});
	$gpsAcqSoundEntry->set_sensitive($gpsSoundButton->active());
	$gpsLostSoundEntry->set_sensitive($gpsSoundButton->active());
	my $gpsSoundHBox = new Gtk::HBox($true, 0);
	$gpsSoundHBox->pack_start($gpsSoundButton, $true, $true, 20);
	$gpsSoundHBox->pack_end($gpsEntryVBox, $true, $true, 20);

	# Traffic
	my $trafficSoundEntry = new Gtk::Entry();
	$self->{'trafficSoundEntry'} = $trafficSoundEntry;
	$trafficSoundEntry->set_text($preferences->getPref('trafficSound'));
	$trafficSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $junkTrafficSoundEntry = new Gtk::Entry();
	$self->{'junkTrafficSoundEntry'} = $junkTrafficSoundEntry;
	$junkTrafficSoundEntry->set_text($preferences->getPref('junkTrafficSound'));
	$junkTrafficSoundEntry->signal_connect('changed', sub{$self->changedHandler()});
	my $trafficEntryVBox = new Gtk::VBox($true, 0);
	$trafficEntryVBox->pack_start($trafficSoundEntry, $false, $false, 0);
	$trafficEntryVBox->pack_start($junkTrafficSoundEntry, $false, $false, 0);
	my $trafficSoundButton = new Gtk::CheckButton('Traffic/Junk traffic sound');
	$trafficSoundButton->set_active($preferences->getPref('trafficSoundEnabled'));
	$self->{'trafficSoundEnabledButton'} = $trafficSoundButton;
	$trafficSoundButton->signal_connect('toggled', sub {$trafficSoundEntry->set_sensitive($trafficSoundButton->active());
		$junkTrafficSoundEntry->set_sensitive($trafficSoundButton->active()); $self->changedHandler()});
	$trafficSoundEntry->set_sensitive($trafficSoundButton->active());
	$junkTrafficSoundEntry->set_sensitive($trafficSoundButton->active());
	my $trafficSoundHBox = new Gtk::HBox($true, 0);
	$trafficSoundHBox->pack_start($trafficSoundButton, $true, $true, 20);
	$trafficSoundHBox->pack_end($trafficEntryVBox, $true, $true, 20);

	# Pack all
	my $soundVBox = new Gtk::VBox($true, 0);
	$soundVBox->pack_start($playBinaryHBox, $false, $false, 0);
	$soundVBox->pack_start($networkSoundHBox, $false, $false, 0);
	$soundVBox->pack_start($alertSoundHBox, $false, $false, 0);
	$soundVBox->pack_start($gpsSoundHBox, $false, $false, 0);
	$soundVBox->pack_start($trafficSoundHBox, $false, $false, 0);
	my $soundFrame = new Gtk::Frame('Sound');
	$soundFrame->add($soundVBox);
	my $tabLabel2 = new Gtk::Label('Sound');
	$propertyBox->append_page($soundFrame, $tabLabel2);

	# Show widget
	$propertyBox->show_all();

	return $self;
}

#
# Destroy handler
#
sub destroyHandler
{
	my $self = shift;
	$self->{'gKismetApplication'}->dialogClose('PrefsDialog');
}

#
# Handle a change to a widget
#
sub changedHandler
{
	my $self = shift;
	$self->{'propertyBox'}->changed();
	return($false);
}

#
# Applied handler
#
sub applyHandler
{
	my $self = shift;
	my $widget = shift;
	my $page = shift;

	if($page != -1)
	{
		return($false);
	}

	my $preferences = $self->{'gKismetApplication'}->{'preferences'};
	for my $f ($preferences->getPref('networkFieldNames'))
	{
		$preferences->setPref('networkFieldStatus', $f, $self->{'networkFieldButton'}{$f}->get_active());
	}
	for my $f ($preferences->getPref('clientFieldNames'))
	{
		$preferences->setPref('clientFieldStatus', $f, $self->{'clientFieldButton'}{$f}->get_active());
	}
	$preferences->setPref('packetDepth', $self->{'packetSpinButton'}->get_value_as_int());
	$preferences->setPref('stringDepth', $self->{'stringSpinButton'}->get_value_as_int());
	$preferences->setPref('units', $self->{'imperialButton'}->get_active() ? 'imperial' : 'metric');
	$preferences->setPref('keepSorted', $self->{'keepSortedButton'}->get_active() ? $true : $false);
	$preferences->setPref('playBinary', $self->{'playBinaryEntry'}->get_text());
	$preferences->setPref('networkSoundEnabled', $self->{'networkSoundEnabledButton'}->get_active());
	$preferences->setPref('newNetworkSound', $self->{'networkSoundEntry'}->get_text());
	$preferences->setPref('alertSoundEnabled', $self->{'alertSoundEnabledButton'}->get_active());
	$preferences->setPref('alertSound', $self->{'alertSoundEntry'}->get_text());
	$preferences->setPref('gpsSoundEnabled', $self->{'gpsSoundEnabledButton'}->get_active());
	$preferences->setPref('gpsAcqSound', $self->{'gpsAcqSoundEntry'}->get_text());
	$preferences->setPref('gpsLostSound', $self->{'gpsLostSoundEntry'}->get_text());
	$preferences->setPref('trafficSoundEnabled', $self->{'trafficSoundEnabledButton'}->get_active());
	$preferences->setPref('trafficSound', $self->{'trafficSoundEntry'}->get_text());
	$preferences->setPref('junkTrafficSound', $self->{'junkTrafficSoundEntry'}->get_text());
	$preferences->save();
	return($false);
}

#
# Get widget
#
sub getWidget
{
	my $self = shift;
	return $self->{'propertyBox'};
}

1;
